package com.huawei.opengl_isrc.slice;

import com.huawei.opengl_isrc.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.render.opengl.GLES30;
import ohos.opengl.ETC1;

public class EGLSlice extends AbilitySlice {
    /**
     * @Author: DY
     * @Date: 2020/12/31
     */


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //EGL的示例
        super.setUIContent(ResourceTable.Layout_Demo_EGL);//切换界面
        Text a=(Text) findComponentById(ResourceTable.Id_text_helloworld);//本行和下一行为ETC1测试
        a.setText("测试ETC1中的方法："+ETC1.getEncodedDataSize(10,5));//输出48则成功
        initView();//(EGL测试)中间将显示一个紫色正方形。

        //返回按钮
        findComponentById(ResourceTable.Id_tab_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent secondIntent = new Intent();
                secondIntent.setOperation(new Intent.OperationBuilder()
                        .withBundleName("com.huawei.opengl_isrc")
                        .withAbilityName("com.huawei.opengl_isrc.MainAbility")
                        .build());
                startAbility(secondIntent);
            }
        });

    }
    //EGL示例代码
    private void initView() {
        SurfaceProvider surfaceProvider =(SurfaceProvider)findComponentById(ResourceTable.Id_sv);
        surfaceProvider.pinToZTop(true);
        surfaceProvider.getSurfaceOps().get().addCallback(new SurfaceOps.Callback() {
            EGLHelper eglHelper = new EGLHelper();
            @Override
            public void surfaceCreated(SurfaceOps surfaceOps) {
            }

            @Override
            public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        eglHelper.start(surfaceOps.getSurface());
                        while (true) {
                            GLES30.glViewport(0, 0, i1, i2);
                            GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
                            GLES30.glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
                            eglHelper.swapBuffers();
                            try {
                                Thread.sleep(16);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }.start();
            }
            @Override
            public void surfaceDestroyed(SurfaceOps surfaceOps) {
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}